package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckItemDeleteFailException;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    // 分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        List<Address> addressList;
        if(queryString == null || "".equals(queryString)){
            addressList = addressDao.getAllAddress();
        }else{
            addressList = addressDao.findAddressByQueryString(queryString);
        }
        PageInfo pageInfo = new PageInfo(addressList);
        return new PageResult(pageInfo.getTotal(),addressList);
    }

    @Override
    public Result add(Address address) {
        try {
            addressDao.insertToAddress(address);
            return new Result(true,"新增机构地址成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增机构地址失败");
        }
    }

    @Override
    public Result delete(Integer id) {
        try{
            Integer count = addressDao.findByAddressId(id);
            if(count != null && count == 0) {
                addressDao.deleteToAddress(id);
                return new Result(false, "删除构地址成功");
            }else{
                throw new CheckItemDeleteFailException();
            }
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof CheckItemDeleteFailException){
                return new Result(false, "已经有人预约该地址，删除失败");
            }
            return new Result(false,"删除构地址失败");
        }
    }

    @Override
    public Result findByid(Integer addressId) {
        try {
            Address address = addressDao.findByid(addressId);
            return new Result(true,"查询地址信息成功",address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询具体地址信息失败");
        }
    }

    @Override
    public Result update(Address address) {
        try {
            addressDao.update(address);
            return new Result(false,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

    @Override
    public List<String> findAll() {
            List<String> all = addressDao.findAll();
            System.out.println(all);
            return all;
    }
}
